//------------------------------------------------------------------------------
// Copyright (c) 2008 www.dnaide.com
// Licensed under the MIT (MIT-LICENSE.txt)
//-------------------------------------------------------------------------------------
using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;
using System.Web;
using DNAide.Core;
using DNAide.IO;

namespace DNAide.Web
{
	public static partial class RequestAide
	{

		public static bool IsGZipSupported()
		{
			return IsGZipSupported(HttpContext.Current);
		}

		public static bool IsGZipSupported(HttpContext context)
		{
			// Check for buggy versions of Internet Explorer
			if (context.Request.Browser.Browser == "IE")
			{
				if (context.Request.Browser.MajorVersion < 6)
					return false;
				else if (context.Request.Browser.MajorVersion == 6 &&
						!string.IsNullOrEmpty(context.Request.ServerVariables["HTTP_USER_AGENT"]) &&
						context.Request.ServerVariables["HTTP_USER_AGENT"].Contains("EV1"))
					return false;
			}

			if (!string.IsNullOrEmpty(context.Request.ServerVariables["HTTP_ACCEPT_ENCODING"]))
			{
				if (context.Request.ServerVariables["HTTP_ACCEPT_ENCODING"].ToLower().Contains("gzip"))
				{
					return true;
				}
			}

			return false;
		}


		public static bool IsDeflateSupported()
		{
			return IsDeflateSupported(HttpContext.Current);
		}

		public static bool IsDeflateSupported(HttpContext context)
		{
			// Check for buggy versions of Internet Explorer
			if (context.Request.Browser.Browser == "IE")
			{
				if (context.Request.Browser.MajorVersion < 6)
					return false;
				else if (context.Request.Browser.MajorVersion == 6 &&
						!string.IsNullOrEmpty(context.Request.ServerVariables["HTTP_USER_AGENT"]) &&
						context.Request.ServerVariables["HTTP_USER_AGENT"].Contains("EV1"))
					return false;
			}

			if (!string.IsNullOrEmpty(context.Request.ServerVariables["HTTP_ACCEPT_ENCODING"]))
			{
				if (context.Request.ServerVariables["HTTP_ACCEPT_ENCODING"].ToLower().Contains("deflate"))
				{
					return true;
				}
			}

			return false;
		}


		public static bool IsCachedOnBrowser(string etag)
		{
			return IsCachedOnBrowser(HttpContext.Current, etag);
		}

		public static bool IsCachedOnBrowser(HttpContext context, string etag)
		{
			if (!string.IsNullOrEmpty(context.Request.ServerVariables["HTTP_IF_NONE_MATCH"]) &&
					context.Request.ServerVariables["HTTP_IF_NONE_MATCH"].Equals(etag))
			{
				return true;
			}
			return false;
		}

	}
}
